<template>
	<view class="index">
		
		<view class="cu-timeline">
			<view class="cu-time" style="width:750upx" v-html="time">{{time}}</view>
			<view class="cu-item" v-for="(list, index) in lists" :key="index">
				<view class="content">
					<view class="cu-capsule radius">
						<view class="cu-tag bg-cyan">上午</view>
						<view class="cu-tag line-cyan">{{ list.created_at }} </view>
					</view>
					<view class="margin-top">
						【{{list.title}}】
						{{list.content.raw}}
					 </view>
				</view>
			</view>
			
		</view>
	
		<view class="cu-load bg-grey" :class="!isLoad?'loading':'over'"></view>
		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				time:'',
				img:'',
				refreshing: false,
				lists: [],
				fetchPageNum: 1,
				isLoad:false,
			}
		},
		onShareAppMessage() {
			return {
				title: "好文分享",
				path:  '/pages/imgview/imgview?id='+this.img,
				imageUrl:this.img
			}
		},
		onLoad() {
			      Number.prototype.getWeekName = function(){
            switch(parseInt(this)){
                case 0:
                    return "星期日";
                case 1:
                    return "星期一";
                case 2:
                    return "星期二";
                case 3:
                    return "星期三";
                case 4:
                    return "星期四";
                case 5:
                    return "星期五";
                case 6:
                    return "星期六";
                default:
                    return "unknow";
            }
        }
	setInterval(() => {
		var d = new Date();
		   var month = d.getMonth()+1;
		this.time ='今天 '+month +'月'+ d.getDate()+ '日' +'  '+d.getDay().getWeekName();
	}, 1000);
			this.getData();
			uni.getProvider({
				service: "share",
				success: (e) => {
					let data = [];
					for (let i = 0; i < e.provider.length; i++) {
						switch (e.provider[i]) {
							case 'weixin':
								data.push({
									name: '分享到微信好友',
									id: 'weixin'
								})
								data.push({
									name: '分享到微信朋友圈',
									id: 'weixin',
									type: 'WXSenceTimeline'
								})
								break;
							case 'qq':
								data.push({
									name: '分享到QQ',
									id: 'qq'
								})
								break;
							default:
								break;
						}
					}
					this.providerList = data;
				},
				fail: (e) => {
					console.log("获取登录通道失败", e);
				}
			});
		},
		
		onPullDownRefresh() {
			console.log("下拉刷新");
			this.refreshing = true;
			this.getData();
		},
		onReachBottom() {
			this.getData();
		},
		methods: {
			cook(url){
				console.log(url);
				this.img=url;
			},
			loc(url){
				uni.navigateTo({
					url: '../imgview/imgview?id='+url,
				})
			},
			getData() {
				uni.request({
					url: 'https://www.btxiaobai.com/xem/cdHour?pageSize=' + (this.refreshing ? 1 : this.fetchPageNum),
					success: (ret) => {
						if (ret.statusCode !== 200) {
							console.log("请求失败:", ret)
						} else {
							if (this.refreshing && ret.data.data[0].id === this.lists[0][0].id) {
								uni.showToast({
									title: "已经最新",
									icon: "none",
								})
								this.refreshing = false;
								uni.stopPullDownRefresh()
								return;
							}
							
							let lists = ret.data.data;
							console.log(lists);
							if (this.refreshing) {
								this.refreshing = false;
								uni.stopPullDownRefresh()
								this.lists = lists;
								this.fetchPageNum = 2;
							} else {
								this.lists = this.lists.concat(lists);
								this.fetchPageNum += 1;
							}
						}
					}
				});
			},
			share(e) {
				if (this.providerList.length === 0) {
					uni.showModal({
						title: "当前环境无分享渠道!",
						showCancel: false
					})
					return;
				}
				let itemList = this.providerList.map(function (value) {
					return value.name
				})
				uni.showActionSheet({
					itemList: itemList,
					success: (res) => {
						uni.share({
							provider: this.providerList[res.tapIndex].id,
							scene: this.providerList[res.tapIndex].type && this.providerList[res.tapIndex].type === 'WXSenceTimeline' ? 'WXSenceTimeline' : "WXSceneSession",
							type: 2,
							title:"比特币小白快讯：最有价值的快讯输出者",
							summary: e.title,
							imageUrl:e.img_src,
							href:"https://www.btxiaobai.com",
							success: (res) => {
								console.log("success:" + JSON.stringify(res));
							},
							fail: (e) => {
								uni.showModal({
									content: e.errMsg,
									showCancel:false
								})
							}
						});
					}
				})
			}
		}
	}
</script>
<style>
	@import "../../common/uni.css";
	
	.index {
		flex: 1;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width:750upx;
		margin-top: 80upx;
	}	
	.row {
		flex-direction: row;
	}
	
	.column {
		flex-direction: column;
	}
</style>